package com.ruoyi.novel.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.novel.domain.User;
import com.ruoyi.novel.service.IUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.novel.domain.Brokerage;
import com.ruoyi.novel.service.IBrokerageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 佣金记录Controller
 *
 * @author 张三
 * @date 2025-06-11
 */
@RestController
@RequestMapping("/novel/brokerage")
public class BrokerageController extends BaseController
{
    @Autowired
    private IBrokerageService brokerageService;

    @Autowired
    private IUserService userService;

    @GetMapping("/getUserInfo")
    public AjaxResult getUserInfo(){
        Long userId = SecurityUtils.getUserId();
        return AjaxResult.success(userService.getById(userId));
    }

    /**
     * 查询佣金记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Brokerage brokerage)
    {
        startPage();
        List<Brokerage> list = brokerageService.selectBrokerageList(brokerage);
        return getDataTable(list);
    }

    /**
     * 导出佣金记录列表
     */
    @PreAuthorize("@ss.hasPermi('novel:brokerage:export')")
    @Log(title = "佣金记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Brokerage brokerage)
    {
        List<Brokerage> list = brokerageService.selectBrokerageList(brokerage);
        ExcelUtil<Brokerage> util = new ExcelUtil<Brokerage>(Brokerage.class);
        util.exportExcel(response, list, "佣金记录数据");
    }

    /**
     * 获取佣金记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('novel:brokerage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(brokerageService.selectBrokerageById(id));
    }

    /**
     * 新增佣金记录
     */
    @PreAuthorize("@ss.hasPermi('novel:brokerage:add')")
    @Log(title = "佣金记录", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody Brokerage brokerage)
    {
        Long userId = SecurityUtils.getUserId();
        brokerage.setUserId(Math.toIntExact(userId));
        User user = userService.getById(userId);
        if(user == null){
            return AjaxResult.error("用户信息错误");
        }
        if(user.getMoney().doubleValue() < brokerage.getMoney().doubleValue()){
            return AjaxResult.error("余额不足");
        }
        userService.update(new UpdateWrapper<User>().eq("id",user.getId()).setSql("money = money -"+ brokerage.getMoney()));
        brokerage.setState(1);
        return toAjax(brokerageService.insertBrokerage(brokerage));
    }

    /**
     * 修改佣金记录
     */
    @PreAuthorize("@ss.hasPermi('novel:brokerage:edit')")
    @Log(title = "佣金记录", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody Brokerage brokerage)
    {
        if(brokerage.getState() == 3){
            User user = userService.getById(brokerage.getUserId());
            if(user == null){
                return AjaxResult.error("用户信息错误");
            }
            Brokerage b = brokerageService.getById(brokerage.getId());
            userService.update(new UpdateWrapper<User>().eq("id",user.getId()).setSql("money = money +"+ b.getMoney()));
        }
        return toAjax(brokerageService.updateBrokerage(brokerage));
    }

    /**
     * 删除佣金记录
     */
    @PreAuthorize("@ss.hasPermi('novel:brokerage:remove')")
    @Log(title = "佣金记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(brokerageService.deleteBrokerageByIds(ids));
    }
}
